package ru.nsu.effel.persistence;

import java.util.List;

import org.hibernate.Session;

public class AccountManager {
	private static AccountManager instance = null;
	
	public static AccountManager getInstance() {
		if (null == instance) {
			instance = new AccountManager();
		}
		return instance;
	}
	
	private AccountManager() {
		
	}
	
	public List getCredentials() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		List credentials = session.createSQLQuery("select id, login, password, status from accounts").list();
		session.getTransaction().commit();
		session.close();
		return credentials;
	}
	
	public Account getAccountById(int id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Account account = (Account)session.get(Account.class, id);
		session.getTransaction().commit();
		session.close();
		return account;
	}
	
	public void addAccount(Account account) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(account);
		session.getTransaction().commit();
		session.close();
	}
}
